<template>
    <div>
        <h3>创建商品</h3>
        <el-row>
            <el-col :span="8"></el-col>
            <el-col :span="8">
                <el-form :model="this.form" label-width="120px">
                    <el-form-item label="商品名">
                        <el-input v-model="this.form.title"></el-input>
                    </el-form-item>
                    <el-form-item label="商品价格">
                        <el-input v-model="this.form.price"></el-input>
                    </el-form-item>
                    <el-form-item label="商品描述">
                        <el-input v-model="this.form.description"></el-input>
                    </el-form-item>
                    <el-form-item label="商品库存">
                        <el-input v-model="this.form.stock"></el-input>
                    </el-form-item>
                    <el-form-item label="商品图片">
                        <el-input v-model="this.form.imgUrl"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="itemCreate()">创建</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
export default {
    data() {
        return {
            form: {
                title: '',
                price: '0',
                description: '',
                stock: '0',
                imgUrl: '',
            }
        }
    },
    methods: {
        itemCreate() {
            if (this.form.title == "" || this.form.title == null) {
                alert("商品名称不能为空");
                return false;
            };
            if (this.form.price == "" || this.form.price == null) {
                alert("商品价格不能为空");
                return false;
            };
            if (this.form.description == "" || this.form.description == null) {
                alert("商品描述不能为空");
                return false;
            };
            if (this.form.stock == null || this.form.stock== "" ) {
                alert("库存不能为空");
                return false;
            };
            if (this.form.imgUrl == "" || this.form.imgUrl == null) {
                alert("商品图片不能为空");
                return false;
            };
            this.$http({
                method: 'POST',
                url:this.$backend + "/item/create",  
                data: this.form,
                withCredentials: true,
            })
            .then((result) => {
                if (result.data.status == "success") {
                    alert("创建成功");
                    this.$router.go(0);
                } else {
                    console.log(result);
                    alert("创建失败,"+ result.data.data.errMsg);
                }
            }).catch((err) => {
                console.log(err)
                alert("发送信息超时, 请稍后再试");
                return false;
            });
        }
    },
}

</script>
